package com.crawler.waf.utils;

import org.springframework.http.HttpStatus;

import com.crawler.waf.exceptions.extendExceptions.WafSimpleException;
import com.crawler.waf.security.authens.BaseUserInfo;
import com.crawler.waf.security.authens.UserToken;
import com.crawler.waf.support.WafContext;

/**
 * UC 工具
 * @author rubekid
 * @date 2016年7月12日
 */
public class UcManager {
	
	/**
	 * 获取当前用户
	 * @return
	 */
	public static Long getUserId(){
		return getUserId(false);
	}
	
	
	/**
	 * 获取当前用户id（允许游客）
	 * @return
	 */
	public static Long getUserId(boolean anonymous){
		BaseUserInfo baseUserInfo = WafContext.getCurrertUserInfo();
		if(baseUserInfo == null){
			if(anonymous){
				return null;
			}
			throw new WafSimpleException(HttpStatus.FORBIDDEN, "WAF/ACCESS_DENIED", "请登录后操作");
		}
		return baseUserInfo.getUserId();
	}
	
	/**
	 * 获取应用ID
	 * @return
	 */
	public static String getAppId(){
		BaseUserInfo baseUserInfo = WafContext.getCurrertUserInfo();
		if(baseUserInfo == null){
			throw new WafSimpleException(HttpStatus.FORBIDDEN, "WAF/ACCESS_DENIED", "请登录后操作");
		}
		return baseUserInfo.getAppId();
	}
	
	
	/**
	 * 返回Token
	 * @return
	 */
	public static UserToken  getUcToken(){
		return WafContext.getCurrentToken(); 
	}
	
	/**
	 * 返回用户信息
	 * @return
	 */
	public static BaseUserInfo getUserInfo(){
		return WafContext.getCurrertUserInfo();
	}
}